package com.hdaccp.hadoop.mr1;

import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;

public class HadoopDataType {
	/**
	 * 使用java.lang.String
	 */
	public static void testString() {
		String s = "hello hi haddop!";
		System.out.println(s.length());
		System.out.println(s.indexOf("a"));
		System.out.println(s);
	}

	/**
	 * 使用Text
	 */
	public static void testText() {
		Text t = new Text("hello hi hadoop!");
		System.out.println(t.getLength());
		System.out.println(t.find("a"));
		System.out.println(t.toString());
	}

	/**
	 * 使用ArrayWritable
	 */
	public static void testArrayWritable() {
		ArrayWritable arr = new ArrayWritable(IntWritable.class);
		IntWritable i = new IntWritable(2010);
		IntWritable j = new IntWritable(8);
		IntWritable k = new IntWritable(30);
		arr.set(new IntWritable[] { i, j, k });
		System.out.println(String.format("i=%d,j=%d,k=%d",
				((IntWritable) arr.get()[0]).get(),
				((IntWritable) arr.get()[1]).get(),
				((IntWritable) arr.get()[2]).get()));
	}

	/**
	 * 使用MapWritable
	 */
	public static void testMapWritable() {
		MapWritable map = new MapWritable();
		Text k1 = new Text("name");
		Text v1 = new Text("jason");
		Text k2 = new Text("password");
		map.put(k1, v1);
		map.put(k2, NullWritable.get());
		System.out.println(map.get(k1).toString());
		System.out.println(map.get(k2).toString());
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		testString();
		testText();
		testArrayWritable();
		testMapWritable();
	}

}
